package com.locationtracker.plus.app.util;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间相关工具类
 */
public class TimeUtils {
    public final static long MinLong = 60 * 1000L;
    public final static long HourLong = 60 * 60 * 1000L;
    public final static long DayLong = 24 * 60 * 60 * 1000L;
    public final static long WeekLong = 24 * 60 * 60 * 7 * 1000L;
    public final static long MonthLong = 24 * 60 * 60 * 30 * 1000L;
    public final static long YearLong = 24 * 60 * 60 * 30 * 12 * 1000L;


    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";


    public final static String FORMAT_1 = "M月d日";
    public final static String FORMAT_2 = "MM月dd日";
    public final static String FORMAT_3 = "yy年MM月dd日";
    public final static String FORMAT_4 = "yyyy年MM月dd日";
    public final static String FORMAT_5 = "M-d";
    public final static String FORMAT_6 = "MM-dd";
    public final static String FORMAT_7 = "yy-MM-dd";
    public final static String FORMAT_8 = "yyyy-MM-dd";
    public final static String FORMAT_9 = "yyyy年MM月";
    public final static String FORMAT_10 = "yyyy-MM";
    public final static String FORMAT_11 = "yyyy";

    public final static String FORMAT_WITH_TIME_1 = "M月d日 HH:mm";
    public final static String FORMAT_WITH_TIME_2 = "MM月dd日 HH:mm";
    public final static String FORMAT_WITH_TIME_3 = "yy年MM月dd日 HH:mm";
    public final static String FORMAT_WITH_TIME_4 = "yyyy年MM月dd日 HH:mm";
    public final static String FORMAT_WITH_TIME_5 = "HH:mm";
    public final static String FORMAT_WITH_TIME_6 = "M-d HH:mm";
    public final static String FORMAT_WITH_TIME_7 = "MM-dd HH:mm";
    public final static String FORMAT_WITH_TIME_8 = "yy-MM-dd HH:mm";
    public final static String FORMAT_WITH_TIME_9 = "yyyy-MM-dd HH:mm";
    public final static String FORMAT_WITH_TIME_10 = "M月d日 HH:mm:ss";
    public final static String FORMAT_WITH_TIME_11 = "MM月dd日 HH:mm:ss";
    public final static String FORMAT_WITH_TIME_12 = "yy年MM月dd日 HH:mm:ss";
    public final static String FORMAT_WITH_TIME_13 = "yyyy年MM月dd日 HH:mm:ss";
    public final static String FORMAT_WITH_TIME_14 = "HH:mm:ss";
    public final static String FORMAT_WITH_TIME_15 = "M-d HH:mm:ss";
    public final static String FORMAT_WITH_TIME_16 = "MM-dd HH:mm:ss";
    public final static String FORMAT_WITH_TIME_17 = "yy-MM-dd HH:mm:ss";
    public final static String FORMAT_WITH_TIME_18 = "yyyy-MM-dd HH:mm:ss";
    public final static String FORMAT_WITH_TIME_19 = "MM/dd HH:mm";
    public final static String FORMAT_WITH_TIME_20 = "yy/MM/dd HH:mm";
    public final static String FORMAT_WITH_TIME_21 = "yyyy.MM.dd";
    public final static String FORMAT_WITH_TIME_22 = "yyyy.MM.dd HH:mm";
    public final static String FORMAT_WITH_TIME_23 = "MM月dd日 EEEE HH:mm";

    public final static String FORMAT_DATETIME_NONE = "yyyyMMdd";
    public static final String FORMAT_DATETIME_NONE_SSS = "yyyyMMddHHmmss";

    public static final String FORMAT_COUNTDOWN = "00 : mm : ss";

    public static final String DEFAULT_FORMAT = FORMAT_WITH_TIME_18;

    public static String timeStamp2String(long seconds) {
        return timeStamp2String(seconds, null);
    }

    public static String timeStamp2String(long seconds, String format) {
        if (TextUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        return sdf.format(new Date(getMillis(seconds)));
    }

    public static String date2String(Date date, String format) {
        if (TextUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        return sdf.format(date);
    }

    public static Date string2Date(String dateStr, String format) {
        if (TextUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }
        Date d;
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        try {
            sdf.setLenient(false);
            d = sdf.parse(dateStr);
        } catch (Exception e) {
            d = null;
        }
        return d;
    }

    /**
     * 根据和当前时间对比，返回对应的日期
     *
     * @param time
     * @return
     */
    public static String getStringType(long time) {
        if (isToday(time)) {
            return timeStamp2String(time, FORMAT_WITH_TIME_5);
        } else if (isThisMonth(time)) {
            return timeStamp2String(time, FORMAT_WITH_TIME_7);
        } else if (isThisYear(time)) {
            return timeStamp2String(time, FORMAT_WITH_TIME_7);
        } else {
            return timeStamp2String(time, FORMAT_WITH_TIME_9);
        }
    }

    public static boolean isToday(long time) {
        return isThisTime(time, FORMAT_8);
    }

    public static boolean isThisWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(getMillis(time)));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        return paramWeek == currentWeek;
    }

    public static boolean isThisMonth(long time) {
        return isThisTime(time, FORMAT_10);
    }

    public static boolean isThisYear(long time) {
        return isThisTime(time, FORMAT_11);
    }

    public static boolean isThisTime(long time, String format) {
        Date date = new Date(getMillis(time));
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        String param = sdf.format(date);
        String now = sdf.format(new Date());
        return param.equals(now);
    }

    public static long getMillis(long time) {
        String timeString = String.valueOf(time);
        if (timeString.length() > 10) {
            return time;
        } else {
            return time * 1000;
        }
    }

    /**
     * 返回多少秒/分钟/小时/天/月/年前
     *
     * @param time 时间戳，支持秒级/毫秒级
     */
    public static String format1(long time) {
        long delta = new Date().getTime() - getMillis(time);
        if (delta < MinLong) {
            return "刚刚";
        }
        if (delta < HourLong) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < DayLong) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < MonthLong) {
            long days = toDays(delta);
            return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
        }
        if (delta < YearLong) {
            long months = toMonths(delta);
            return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
        }
        long years = toYears(delta);
        return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
    }

    public static String format(long time) {
        long delta = new Date().getTime() - getMillis(time);
        if (delta < MinLong) {
            long seconds = toSeconds(delta);
            //return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
            return "刚刚";
        }
        if (delta < HourLong) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < DayLong) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        return timeStamp2String(time);
    }

    public static String formatTime(long time) {
        StringBuilder sb = new StringBuilder();
        if (time > TimeUtils.DayLong) {
            long day = time / TimeUtils.DayLong;
            sb.append(day + "天 ");
        }
        long hour = time % TimeUtils.DayLong / TimeUtils.HourLong;
        if (hour < 10) {
            sb.append("0");
        }
        sb.append(hour);
        sb.append(":");
        long min = time % TimeUtils.DayLong % TimeUtils.HourLong / TimeUtils.MinLong;
        if (min < 10) {
            sb.append("0");
        }
        sb.append(min);
        sb.append(":");
        long seconds = time % TimeUtils.DayLong % TimeUtils.HourLong % TimeUtils.MinLong / 1000;
        if (seconds < 10) {
            sb.append("0");
        }
        sb.append(seconds);
        return sb.toString();
    }

    public static String formatTime1(long time) {
        StringBuilder sb = new StringBuilder();
        long hour = time / TimeUtils.HourLong;
        if (hour < 10) {
            sb.append("0");
        }
        sb.append(hour);
        sb.append(":");
        long min = time % TimeUtils.DayLong % TimeUtils.HourLong / TimeUtils.MinLong;
        if (min < 10) {
            sb.append("0");
        }
        sb.append(min);
        sb.append(":");
        long seconds = time % TimeUtils.DayLong % TimeUtils.HourLong % TimeUtils.MinLong / 1000;
        if (seconds < 10) {
            sb.append("0");
        }
        sb.append(seconds);
        return sb.toString();
    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }
}